import React from 'react';
import CardInfo from './cardInfo.js';
import BaseInfo from './baseInfo.js';
import PersonInfo from './person_info.js';
import SafeInfo from './safeInfo.js';
import SourceInfo from './sourceinfo.js';
import {set,get} from './../../store';
import {getuser} from './../../api';
import qs from 'qs';

export default class Card extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    	target: 'info',
      user:{
        userinfo:{
          headpic:''
        }
      }
    }
    this.goto = this.goto.bind(this);
  }

  componentWillMount() {
    const userkey = '_user';
    let userObj = get(userkey);
    let online = get('login_status');
    if(online=='true') {
      if(userObj) {
        this.setState({
          user: JSON.parse(userObj),
        });
      } else {
        console.info("正在加载用户信息");
        getuser().then(resp=>{
          console.info(resp);
          if(resp.status==200) {
            set(userkey,JSON.stringify(resp.content));
            this.setState({
              user: JSON.stringify(resp.content)
            });
            window.location.reload();
          }
        }).catch(err=>{
          console.info(err);
        }) 
      }
    } else {
      console.info('无效的登录，请重新登录');
      let curl = window.location.href;
      window.location.href='/login?target='+curl;
    }
  }

  goto(e) {
  	this.setState({
  		target: e 
  	});
  }

  render() {
    return (
      <div className="card_p">
      	<div className="card_info_left">
      		<CardInfo user={this.props.user} goto={this.goto} target={this.state.target}></CardInfo>
      	</div>
      	<div className="card_info_right">
      		<div className="user_box_body">
      			<BaseInfo target={this.state.target} name="info">
              <PersonInfo user={this.state.user} />
            </BaseInfo>
            <BaseInfo target={this.state.target} name="safe">
              <SafeInfo user={this.state.user}></SafeInfo>
            </BaseInfo>
            <BaseInfo target={this.state.target} name="source">
              <SourceInfo/>
            </BaseInfo>
      		</div>
{/*          <div className="user_supply_body">
            补充说明
          </div>*/}
          <div className="clear"></div>
      	</div>
      	<div className="clear"></div>
      </div>
    );
  }
}